package com.cloudcc.mobile;

import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Vibrator;
import android.widget.RemoteViews;

import com.baidu.mapapi.SDKInitializer;
import com.cloudcc.cloudframe.CApplication;
import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.EventList;
import com.cloudcc.cloudframe.bus.EventList.PushBindEvent;
import com.cloudcc.cloudframe.bus.EventList.PushRegisterEvent;
import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.mobile.im.IMHelper;
import com.cloudcc.mobile.manager.LocationManager;
import com.cloudcc.mobile.manager.local.LocationService;
import com.cloudcc.mobile.presenter.PushPresenter;
import com.qihoo.linker.logcollector.LogCollector;
import com.qihoo.linker.logcollector.upload.HttpParameters;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Stack;

import cn.sharesdk.framework.ShareSDK;

public class AppContext extends CApplication {

	public static ArrayList<String> selectPhotos = new ArrayList<String>();

	public static Stack<Activity> activityStack;

	/** --------------定位------------ */
	public LocationService locationService;
	public Vibrator mVibrator;
	/** --------------定位------------ */
	@SuppressWarnings("unused")
	@Override
	public void onCreate() {
		EventEngine.register(this);
		super.onCreate();
		ShareSDK.initSDK(this);
//		initCrash();
		CrashHandler.create(this);
		initMap();

		initIM();

		initLocal();
//		initlog();
//		initlanguage();
	}

	private void initlanguage() {
		// TODO Auto-generated method stub
		Configuration config = getResources().getConfiguration();//获取系统的配置  
		config.locale = Locale.ENGLISH;//将语言更改为英文
		getResources().updateConfiguration(config, getResources().getDisplayMetrics());//更新配置  
	}

	private void initlog() {
		final String UPLOAD_URL = "http://xxxxxxxx";
		// TODO Auto-generated method stub
		HttpParameters params = new HttpParameters();
		params.add("key1", "value1");
		params.add("key2", "value2");
		params.add("key3", "value3");
		// .......
		// replace your key and value;

		boolean isDebug = true;
		// set debug mode , you can see debug log , and also you can get logfile
		// in sdcard;
		LogCollector.setDebugMode(isDebug);
		LogCollector.init(getApplicationContext(), UPLOAD_URL, params);// params
																		// can
																		// be
																		// null
		uploadLogFile();
	}

	private void uploadLogFile() {
		boolean isWifiOnly = true;// only wifi mode can upload
		LogCollector.upload(isWifiOnly);// upload at the right time
	}

	private void initCrash() {
		AppException.init();
//        CrashHandler crashHandler = CrashHandler.getInstance();
//        crashHandler.init();
		//CrashReport.initCrashReport(this, "900014565", false);
		CrashReport.initCrashReport(this, "0c8e034d6d", false);
	}
	/**
	 * 初始化地图
	 */
	private void initMap() {
		SDKInitializer.initialize(this);
	}
	/**
	 * IM初始化
	 */
	private void initIM() {
		IMHelper.getInstance().init(this);
	}
	/**
	 * 定位初始化
	 */
	private void initLocal() {
		LocationManager.getInstance().init();
	}

	/**
	 * 获得applicationContext
	 * 
	 * @return Application
	 */
	public static AppContext getInstance() {
		return (AppContext) mInstance;
	}

	/**
	 * 创建挂机图标
	 */
	@SuppressWarnings("deprecation")
	public void showNotification(Intent intent) {
		int icon = R.drawable.icon;
		int appicon = R.drawable.icon;
		CharSequence tickerText = getResources().getString(
				R.string.app_is_run_background);
		long when = System.currentTimeMillis();
		mNotification = new Notification(appicon, tickerText, when);
		// 放置在"正在运行"栏目中
		mNotification.flags = Notification.FLAG_ONGOING_EVENT;

		RemoteViews contentView = new RemoteViews(getPackageName(),
				R.layout.notification);
		contentView.setImageViewResource(R.id.icon, icon);
		contentView.setTextViewText(R.id.title, "CloudCC");
		contentView.setTextViewText(R.id.text, tickerText);
		contentView.setLong(R.id.time, "setTime", when);
		// 指定个性化视图
		mNotification.contentView = contentView;
		// Intent intent = new Intent(Intent.ACTION_MAIN);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 4,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		// 指定内容意图
		mNotification.contentIntent = contentIntent;
		mNotification.flags = Notification.FLAG_AUTO_CANCEL;
		mNotificationManager.notify(4, mNotification);
	}

	// 清空通知
	public void clearNotification(int num) {
		mNotificationManager.cancel(num);
	}

	public void onEvent(PushBindEvent event) {
//		new PushPresenter().registerBind("");
	}

	public void onEventMainThread(PushRegisterEvent event) {
		LogUtils.d("Push", "注册push结果:" + event.isOk());
	}

	public void onEventMainThread(EventList.PushCancleEvent event) {
		LogUtils.d("Push", "取消注册push结果:" + event.isOk());
	}
}
